home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / clonecd / September 93.img / Archives / Utilities / Quicktime / SLINK's VCR QuickTime / MP Source / main.c < prev    next >
C/C++ Source or Header  |  1992-06-15  |  11KB  |  537 lines

  1. /*---------------------------------------------------------------------
  2.     File: main.c
  3.     Purpose: 
  4.     
  5.     Created by: Geoffrey Slinker
  6.     Date: 8:26:41 AM  6/8/92
  7.     Modified: 
  8. ---------------------------------------------------------------------*/
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <oops.h>
  12. #include <Movies.h>
  13.  
  14. #include "Dlog.h"
  15. #include "about_Dlog.h"
  16. #include "Alert_Class.h"
  17. #include "vcr.h"
  18. #include "mycolors.h"
  19. #include "myQuickTime.h"
  20.  
  21. #define NUKE_ALL_EVENTS    0
  22. #define NULL_POINTER NULL
  23.  
  24. #define MY_ALERT    601
  25. //#define VCR_DLOG    700
  26. #define VCR_DLOG    702
  27.  
  28.  
  29. #define BASE_MENU    600
  30. #define APPLE_ID    600
  31. #define FILE_ID        601
  32.  
  33. #define ABOUT_ITEM    1
  34.  
  35.  
  36. #define OPEN_ITEM    1
  37. #define CLOSE_ITEM    2
  38. #define    QUIT_ITEM    4
  39. /*-------------------------
  40. protos
  41. -------------------------*/
  42. void InitMac(void);
  43. void MainLoop(void);
  44. int Eventful(void);
  45. int HandleMouseDown(void);
  46. int HandleMenuChoice (long int choice);
  47. void HandleAppleChoice(int theItem);
  48. int HandleFileChoice(int theItem);
  49. void MenuBarInit(void);
  50. void InitMyStuff(void);
  51. void CleanUpMyStuff(void);
  52. void ClockTask(void);
  53. void MaintainMenus(void);
  54.  
  55. /*-------------------------
  56. globals
  57. -------------------------*/
  58. EventRecord gEvent;
  59. MenuHandle gAppleMenu;
  60. about_Dlog *aboutDlog;
  61. Alert_Class *myAlert;
  62. QT *myQT;
  63. Boolean moviesLoaded = false;
  64. VCR *vcr;
  65. Boolean opened = false;
  66.  
  67. //DateTimeRec oldT;
  68. long oldClockTick; // used for updating vcr clock
  69. extern Boolean blink;
  70. extern long oldTick; // used for updating status
  71. /**********************************************************************/
  72. void main()
  73. {
  74.     InitMac();
  75.     InitMyStuff();
  76.     (*myQT).MovieCheck();  // Is QuickTime available?
  77.     moviesLoaded = true;
  78.     
  79.     MainLoop();
  80.     
  81.     CleanUpMyStuff();
  82. }
  83. /*---------------------------------------------------------------------
  84.     Function: InitMac()
  85.     Purpose: 
  86.     Returns: 
  87.     
  88.     Created by: Geoffrey Slinker
  89.     Date: 8:30:00 AM  6/8/92
  90.     Modified: 
  91. ---------------------------------------------------------------------*/
  92. void InitMac()
  93. {
  94.     InitGraf(&thePort);
  95.     InitFonts();
  96.     FlushEvents(everyEvent,NUKE_ALL_EVENTS);
  97.     InitWindows();  // This calls InitPalettes
  98.     InitMenus();
  99.     TEInit();
  100.     InitDialogs(NULL_POINTER);
  101.     InitCursor();
  102.     MaxApplZone();
  103.     
  104.     /*-------------------------
  105.     Other things related to the MAC
  106.     or the MAC interface that needs
  107.     to be initiated
  108.     -------------------------*/
  109.     MenuBarInit();
  110. }
  111. /*---------------------------------------------------------------------
  112.     Function: void MainLoop()
  113.     Purpose: repeatedly call function with getnext event and very
  114.     large switch statement.
  115.     Returns: 
  116.     
  117.     Created by: Geoffrey Slinker
  118.     Date: 8:33:44 AM  6/8/92
  119.     Modified: 
  120. ---------------------------------------------------------------------*/
  121. void MainLoop()
  122. {
  123.     AppFile theFile;
  124.     int message, count;
  125.     int response = 1;
  126.  
  127.     /*-------------------------
  128.     Was the application launched
  129.     by double click on a file
  130.     of our type?
  131.     
  132.     Since this application does
  133.     not save QT files, this will
  134.     never happen.
  135.     -------------------------*/
  136.     
  137.     CountAppFiles(&message,&count);
  138.     if ( (message == 0) && (count >= 1)  ) {
  139.         GetAppFiles(1,&theFile);
  140.         ClrAppFiles(1);
  141.     } /* end if */
  142.     
  143.  
  144.     while (response == 1){
  145.         response = Eventful();
  146.     }
  147.     
  148. }
  149. /*---------------------------------------------------------------------
  150.     Function: Eventful()
  151.     Purpose: Contains GetNextEvent and the switch statement.
  152.     Returns: 
  153.     
  154.     Created by: Geoffrey Slinker
  155.     Date: 8:35:17 AM  6/8/92
  156.     Modified: 
  157. ---------------------------------------------------------------------*/
  158. int Eventful()
  159. {
  160.     int response = 1;
  161.     char theChar;
  162.     GrafPtr aPort;
  163.     WindowPtr whichWindow;
  164.     Movie myMovie;
  165.     CWindowPtr movieWindow;
  166.     
  167.     
  168.     myMovie = (*myQT).myMovie;
  169.     movieWindow = (*myQT).movieWindow;
  170.     
  171.     
  172.     SystemTask();
  173.     ClockTask();
  174.     (*myQT).MyMoviesTask();
  175.     
  176.     GetNextEvent(everyEvent,&gEvent);
  177.     
  178.     switch ( gEvent.what  ) {
  179.         case mouseDown:
  180.             response = HandleMouseDown();
  181.             break;
  182.         
  183.         case keyDown:
  184.         case autoKey:
  185.             theChar = gEvent.message & charCodeMask;
  186.             if ( (gEvent.modifiers & cmdKey) != 0  ) {
  187.                 response = HandleMenuChoice( MenuKey(theChar));
  188.             }
  189.             else {
  190.                 whichWindow = FrontWindow();
  191.             }
  192.             break;
  193.         
  194.         case activateEvt:
  195.             SetPort( (WindowPtr) gEvent.message );
  196.             InvalRect(&((WindowPtr)gEvent.message)->portRect);
  197.             break;
  198.             
  199.         case updateEvt:
  200.             {
  201.                 Boolean isDaWindow;
  202.                 
  203.                     
  204.                 if ( whichWindow == NULL_POINTER  ) {
  205.                     isDaWindow = false;
  206.                 }
  207.                 else {
  208.                     isDaWindow = (( (WindowPeek)whichWindow)->windowKind < 0);
  209.                 } /* end if then else */
  210.                 
  211.                 if ( !isDaWindow ) 
  212.                 {
  213.                     BeginUpdate((WindowPtr)gEvent.message);
  214.  
  215.                     GetPort(&aPort);
  216.                     SetPort((WindowPtr)gEvent.message);
  217.     
  218.                         if ( (WindowPtr)gEvent.message == (*vcr).myDlog) {
  219.                             (*vcr).RedrawDialog();
  220.                         }
  221.                         
  222.                         if ((CWindowPtr)gEvent.message == movieWindow) {
  223.                             UpdateMovie( myMovie );
  224.                         }
  225.                         
  226.                         if ((CWindowPtr)gEvent.message == (*myQT).moviePosterWindow) {
  227.                             (*myQT).RedrawPoster();
  228.                         }
  229.                         
  230.                         
  231.                     EndUpdate((WindowPtr)gEvent.message);
  232.                     SetPort(aPort);
  233.                 } /* end if */
  234.  
  235.             }
  236.             break;
  237.             
  238.         case nullEvent:
  239.             MaintainMenus();
  240.             break;
  241.                 
  242.         default: 
  243.             response = 1;
  244.             break;
  245.             
  246.     } /* end switch */
  247.  
  248.  
  249.     return(response);
  250. }
  251. /*---------------------------------------------------------------------
  252.     Function: HandleMouseDown
  253.     Purpose: 
  254.     Returns: 
  255.     
  256.     Created by: Geoffrey Slinker
  257.     Date: 8:38:43 AM  6/8/92
  258.     Modified: 
  259. ---------------------------------------------------------------------*/
  260. int HandleMouseDown()
  261. {    
  262.     WindowPtr whichWindow,frontWindow;
  263.     short int thePart;
  264.     long int menuChoice, windSize;
  265.     Rect bRect;
  266.     int response = 1;
  267.     Point thePoint;
  268.     
  269.     thePart = FindWindow(gEvent.where,&whichWindow);
  270.     
  271.     switch ( thePart  ) {
  272.         case inMenuBar:
  273.             menuChoice = MenuSelect(gEvent.where);
  274.             response = HandleMenuChoice(menuChoice);
  275.             break;
  276.         
  277.         case inSysWindow:
  278.             SystemClick(&gEvent,whichWindow);
  279.             break;
  280.         
  281.         case inDrag:
  282.             SelectWindow(whichWindow);
  283.             bRect = screenBits.bounds;
  284.             DragWindow(whichWindow,gEvent.where, &bRect);    
  285.             break;
  286.             
  287.         case inContent:
  288.             frontWindow = FrontWindow();
  289.             SelectWindow(whichWindow);
  290.             SetPort(whichWindow);
  291.             if (whichWindow == (*vcr).myDlog) {
  292.                 (*vcr).HandleDialog(&gEvent);
  293.             }
  294.             break;
  295.             
  296.         case inGoAway:
  297.             HideWindow(whichWindow);
  298.             break;    
  299.         default: response = 1; break;
  300.     } /* end switch */
  301.     
  302.     return(response);
  303. }
  304. /*---------------------------------------------------------------------
  305.     Function: int HandleMenuChoice(long int choice)
  306.     Purpose: Distribute the menu choice to the proper handler.
  307.     Returns: 
  308.     
  309.     Created by: Geoffrey Slinker
  310.     Date: 8:41:11 AM  6/8/92
  311.     Modified: 
  312. ---------------------------------------------------------------------*/
  313. int HandleMenuChoice (long int choice)
  314. {
  315.     int theMenu;
  316.     int    theItem;
  317.     int response;
  318.     char stuff[80];
  319.     Str255 message;
  320.     
  321.     response = 1;
  322.     
  323.     if (choice != 0) {
  324.         theMenu = HiWord(choice);
  325.         theItem = LoWord(choice);
  326.         
  327.         switch ( theMenu  ) {
  328.             case APPLE_ID:
  329.                 HandleAppleChoice(theItem);
  330.                 break;
  331.             
  332.             case FILE_ID: 
  333.                 response = HandleFileChoice(theItem);
  334.                 break;
  335.                 
  336.             default: 
  337.                 sprintf((char *)message,"Menu Bar Item = %d",theItem);
  338.                 (*myAlert).AlertCaution("\pUnimplemented Menu Bar Item",
  339.                                         CtoPstr(message),"\pSorry!",NULL);
  340.                 break;
  341.         } /* end switch */
  342.     } /* end if */
  343.     
  344.     HiliteMenu(0);
  345.     return(response);
  346. }
  347. /*---------------------------------------------------------------------
  348.     Function: HandleAppleChoice
  349.     Purpose: 
  350.     Returns: 
  351.     
  352.     Created by: Geoffrey Slinker
  353.     Date: 8:42:38 AM  6/8/92
  354.     Modified: 
  355. ---------------------------------------------------------------------*/
  356. void HandleAppleChoice(int theItem)
  357. {
  358.     Str255    accName;
  359.     int        accNumber;
  360.     
  361.     switch ( theItem  ) {
  362.         case ABOUT_ITEM:
  363.             (*aboutDlog).HandleDialog(&gEvent);
  364.             break;
  365.         default:
  366.             GetItem(gAppleMenu,theItem,accName);
  367.             accNumber = OpenDeskAcc(accName);
  368.             break;
  369.     } /* end switch */
  370. }
  371. /*---------------------------------------------------------------------
  372.     Function: HandleFileChoice
  373.     Purpose: 
  374.     Returns: 
  375.     
  376.     Created by: Geoffrey Slinker
  377.     Date: 8:44:06 AM  6/8/92
  378.     Modified: 
  379. ---------------------------------------------------------------------*/
  380. int HandleFileChoice(int theItem)
  381. {
  382.     int response;
  383.     WindowPtr whichWindow;
  384.     Str255 message;
  385.     
  386.     response = 1;
  387.     
  388.     switch ( theItem  ) {
  389.         case OPEN_ITEM: 
  390.             opened = (*myQT).OpenMovie();
  391.             break;
  392.             
  393.         case CLOSE_ITEM:
  394.             (*myQT).CloseMovie();
  395.             opened = false;
  396.             break;
  397.             
  398.         case QUIT_ITEM:    response = 0; break;
  399.         default: 
  400.             sprintf((char *)message,"Menu Bar Item = %d",theItem);
  401.                 (*myAlert).AlertCaution("\pUnimplemented Menu Bar Item",
  402.                                         CtoPstr(message),"\pSorry!",NULL);
  403.             break;
  404.     } /* end switch */
  405.     
  406.     return(response);
  407. }
  408. /*---------------------------------------------------------------------
  409.     Function: void MenuBarInit()
  410.     Purpose: Load the menu resources
  411.     Returns: 
  412.     
  413.     Created by: Geoffrey Slinker
  414.     Date: 9:06:34 AM  6/8/92
  415.     Modified: 
  416. ---------------------------------------------------------------------*/
  417. void MenuBarInit()
  418. {
  419.     Handle myBar;    
  420.  
  421.     if ( (myBar = GetNewMBar(BASE_MENU)) == NULL_POINTER  ) {
  422.         ExitToShell();
  423.     } /* end if */
  424.     
  425.     SetMenuBar(myBar);
  426.     
  427.     if ( (gAppleMenu = GetMHandle( APPLE_ID)) == NULL_POINTER  ) {
  428.         ExitToShell();
  429.     } /* end if */
  430.     
  431.     AddResMenu(gAppleMenu,'DRVR');
  432.     
  433.  
  434.     DrawMenuBar();
  435. }
  436. /*---------------------------------------------------------------------
  437.     Function: InitMyStuff();
  438.     Purpose: 
  439.     Returns: 
  440.     
  441.     Created by: Geoffrey Slinker
  442.     Date: 9:16:28 AM  6/8/92
  443.     Modified: 
  444. ---------------------------------------------------------------------*/
  445. void InitMyStuff()
  446. {
  447.     DialogPtr myDlog;
  448.     
  449.     //GetTime(&oldT);
  450.     oldClockTick = oldTick = TickCount();
  451.     //oldTick = TickCount();
  452.     aboutDlog = new about_Dlog;
  453.     (*aboutDlog).loadResource(ABOUT_DLOG);
  454.     
  455.     myAlert = new Alert_Class;
  456.     (*myAlert).ChooseResource(MY_ALERT);
  457.     
  458.     vcr = new VCR;
  459.     (*vcr).loadResource(VCR_DLOG);
  460.     (*vcr).PutUpDialog();
  461.     
  462.     myQT = new QT;
  463.     (*myQT).LoadResources(); 
  464. }
  465. /*---------------------------------------------------------------------
  466.     Function: CleanUpMyStuff()
  467.     Purpose: 
  468.     Returns: 
  469.     
  470.     Created by: Geoffrey Slinker
  471.     Date: 9:16:28 AM  6/8/92
  472.     Modified: 
  473. ---------------------------------------------------------------------*/
  474. void CleanUpMyStuff()
  475. {
  476.     delete aboutDlog;
  477.     delete myAlert;
  478.     delete vcr;
  479.     delete myQT;
  480.     
  481.     if (moviesLoaded) {
  482.         ExitMovies();
  483.     }
  484.     
  485. }
  486. /*---------------------------------------------------------------------
  487.     Function: ClockTask();
  488.     Purpose: 
  489.     Returns: 
  490.     
  491.     Created by: Geoffrey Slinker
  492.     Date: 4:22:18 PM  6/12/92
  493.     Modified: 
  494. ---------------------------------------------------------------------*/
  495. void ClockTask()
  496. {
  497.     long newClockTick;
  498.     Boolean blinky;
  499.     
  500.     blinky = blink;
  501.     newClockTick = TickCount();
  502.  
  503.     if ( (newClockTick - oldClockTick) >= 60) {
  504.     
  505.         blink = !blink;
  506.         oldClockTick = newClockTick;
  507.     }
  508.     if (blink != blinky) (*vcr).RedrawTime();
  509.     (*vcr).UpdateProgress();
  510. }
  511. /*---------------------------------------------------------------------
  512.     Function: void MaintainMenus()
  513.     Purpose: 
  514.     Returns: 
  515.     
  516.     Created by: Geoffrey Slinker
  517.     Date: 10:49:56 AM  6/13/92
  518.     Modified: 
  519. ---------------------------------------------------------------------*/
  520. void MaintainMenus()
  521. {
  522.     WindowPtr frontWindow;
  523.     MenuHandle theMenu;
  524.     DialogPtr myDlog;
  525.  
  526.     theMenu = GetMHandle(FILE_ID);
  527.     
  528.     if (opened) {
  529.         DisableItem(theMenu,OPEN_ITEM);
  530.         EnableItem(theMenu,CLOSE_ITEM);
  531.     }
  532.     else {
  533.         DisableItem(theMenu,CLOSE_ITEM);
  534.         EnableItem(theMenu,OPEN_ITEM);
  535.     }
  536. }
  537.